package com.zch.research.mqtt.consumer.controller;

import com.zch.client.spring.mqtt.client.FastMqttClient;
import com.zch.client.spring.mqtt.dto.MqttSubscribeRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class ConsumerController {
    @Resource
    FastMqttClient fastMqttClient;

    @Value("${spring.mqtt.client.id}")
    private String clientId;

    @RequestMapping("/reConnect")
    @ResponseBody
    public String reConnect(){
        fastMqttClient.connect();
        return clientId + "连接到服务器";
    }

    @RequestMapping("/disConnect")
    @ResponseBody
    public String disConnect(){
        fastMqttClient.disConnect();
        return clientId + "与服务器断开连接";
    }

    @PostMapping("/subscribe")
    @ResponseBody
    public String subscribe(@RequestBody MqttSubscribeRequest subscribeRequest){
        fastMqttClient.subscribe(subscribeRequest);
        return clientId + "与服务器断开连接";
    }

}
